let app = getApp();
const api = require('../../../../ws_micro_shop/utils/api.js');
const { getPermissionKey, getRolesInfo } = require('../../../api/common');
const { qyWXCheckSeesion } = require('../../../../common/utils/ws/ws-wxwork');
const { loadingMiddleware } = require('../../../common/utils/util');

Page({
  data: {
    keyword: '',
    height: wx.systemInfo.windowHeight,
    page: 1,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-15.png',
      text: '今日无客户生日',
    },
    list: [],
    data: {},
    auth: '',
    isMy: false,
    isData: false,
    loadingText: '下拉加载',
    canPullUp: true,
    from_openid: '',
  },

  onLoad() {
    this.setData(
      {
        is_birthday: 1,
        birthday: this.addDate(),
        permissionKey: app.ws_micro_shop_globalData.role,
        from_openid: app.ws_micro_shop_globalData.openid,
        isSaas: app.ws_micro_shop_globalData.isSaas,
      },
      () => {
        this.getList();
      }
    );
  },

  addDate() {
    let date = new Date();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    return date.getFullYear() + '.' + this.getFormatDate(month) + '.' + this.getFormatDate(day);
  },

  // 日期月份/天的显示，如果是1位数，则在前面加上'0'
  getFormatDate(arg) {
    if (!arg) {
      return '';
    }
    let re = String(arg);
    if (re.length < 2) {
      re = '0' + re;
    }
    return re;
  },


  getList() {
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });
    const { list = [], isMy = '', page } = this.data;
    loadingMiddleware.before(this);
    app.ws_micro_shoprequest({
      url: api.staff.clue_index,
      data: {
        page,
        is_my: 1,
        is_birthday: 1,
      },
      success: res => {
        if (res && res.code === 0) {
          const newList = res.data.list;
          if (newList.length <= 0) {
            loadingMiddleware.after(res, this);
            return;
          }
          this.setData({
            list: [...list, ...newList],
          }, () => {
            loadingMiddleware.after(res, this);
          });
        }
      },
      fail: res => {
        loadingMiddleware.after(res, this);
      },
      complete: res => {
        this.setData({
          isData: true,
          pageLoading: true,
        },() => {
          wx.hideLoading();
        });
      },
    });
  },

  onReachBottom() {
    let { canPullUp, page } = this.data;
    if (canPullUp) {
      this.setData(
        {
          page: ++page,
        }, () => {
          this.getList();
        },
      );
    }
  },


  onNavigate(e) {
    let { url = '', ...params } = e.currentTarget.dataset;
    url && app.util.navigateTo(url, params);
  },

  openChat(e) {
    const { wechat_open_id = '', nickname = '', realname = '', external_userid: externalUserId = '' } = e.currentTarget.dataset.item;
    if (!app.isWXWork) {
      this.toLeaveMessage(wechat_open_id,nickname,realname);
      return;
    }

    qyWXCheckSeesion({
      success: () => {
        if (externalUserId) {
          wx.qy.openEnterpriseChat({
            externalUserIds: externalUserId,
            groupName: '',
            success: () => {
              console.log('enterprise chat opened');
            },
          });
        } else {
          this.toLeaveMessage(wechat_open_id,nickname,realname);
        }
      },
      fail: err => {
        console.log('企业微信登录失败', err);
      },
    });
  },

  toLeaveMessage(wechat_open_id = '',nickname = '',realname = '') {
    const { from_openid ='' } = this.data;
    const url = `/ws_micro_shop_guide/pages/message/detail/detail?from_openid=${from_openid}&to_openid=${wechat_open_id}&name=${nickname || realname}`;
    app.util.navigateTo(url);
  }
});
